package com.conero.yangsu;

import com.conero.yangsu.lib.FeedbackData;
import org.hibernate.service.spi.ServiceException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * Link:    https://blog.csdn.net/qq_37495786/article/details/106300052
 *          https://segmentfault.com/a/1190000006749441
 * 错误请求处理
 * 2021年11月25日 星期四
 * 需配置如下才有效
 *      spring.mvc.throw-exception-if-no-handler-found=true
 *      spring.resources.add-mappings=false
 */
@RestControllerAdvice
public class YangsuHandlerError {
    // 页面不存在时
    @ExceptionHandler(NoHandlerFoundException.class)
    public FeedbackData noFoundHandler(NoHandlerFoundException e) {
        System.out.println("error:"+e.getMessage());
        return FeedbackData.error("路径不存在，请您先阅读参考文档！");
    }

    // 服务错误
    @ExceptionHandler(ServiceException.class)
    public FeedbackData serviceErrorHandler(ServiceException e) {
        System.out.println("error:"+e.getMessage());
        return FeedbackData.error("服务错误！");
    }

    // 基础错误
    @ExceptionHandler(Exception.class)
    public FeedbackData baseException(Exception e) {
        System.out.println("error:"+e.getMessage());
        return FeedbackData.error("请求错误，参照信息：" + e.getMessage());
    }
}
